package com.emotion.sshticket.utilitys;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

public class ValueUtil {
	public static String nullToString(String param) throws UnsupportedEncodingException {
		return nullToString(param, "");
	}

	@SuppressWarnings("deprecation")
	public static String nullToString(String param, String defaultValue) throws UnsupportedEncodingException {
		if (param == null || param.equals("")) {
			param = defaultValue;
		} else {
			param = new String(param.getBytes("8859_1"),"UTF-8");
			param = URLDecoder.decode(param);
			param = param.trim();
			param = param.replaceAll("&nbsp", "");
			param = param.replaceAll("\n", "");
			param = param.replaceAll("", "");
		}
		return param;
	}

	public static double nullToDouble(String param) {
		return nullToDouble(param, 0);
	}

	public static double nullToDouble(String param, double defaultValue) {
		double ret = 0;

		if (param != null && isNumberic(param)) {
			ret = Double.valueOf(param);
		} else {
			ret = defaultValue;
		}

		return ret;
	}

	public static int nullToInt(String param) {
		return nullToInt(param, 0);
	}

	public static int nullToInt(String param, int defaultValue) {
		int ret = 0;

		if (param != null && isNumberic(param)) {
			ret = Integer.parseInt(param);
		} else {
			ret = defaultValue;
		}

		return ret;
	}

	public static boolean isNumberic(String param) {
		boolean ret = true;
	    try {
	    	if (param == null) {
	    		ret = false;
	    	} else {
	    		Double.parseDouble(param);
	    	}
	    } catch (NumberFormatException e) {
	        ret = false;
	    }
	    return ret;
	}

	public static String getRequestURLWithParameters(HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Enumeration<String> param = request.getParameterNames();
        StringBuffer sb = new StringBuffer();
        while(param.hasMoreElements()) {
            String key = (String)param.nextElement();
            String value = request.getParameter(key);
            if (value != null && value.equals("") == false) {
            	sb.append(key + "=" + value + "&");
            }
        }
        StringBuffer URL = request.getRequestURL();
        if (sb.length() > 0) {
        	URL.append("?" + sb.toString());
        }
        return URL.toString();
	}

	public static List<String> getAddrList() {
		String[] addrArr = {
			"北海道","青森県","岩手県","宮城県","秋田県","山形県","福島県","茨城県","栃木県","群馬県","埼玉県","千葉県","東京都","神奈川県","新潟県","富山県","石川県","福井県","山梨県","長野県",
			"岐阜県","静岡県","愛知県","三重県","滋賀県","京都府","大阪府","兵庫県","奈良県","和歌山県","鳥取県","島根県","岡山県","広島県","山口県","徳島県","香川県","愛媛県","高知県","福岡県",
			"佐賀県","長崎県","熊本県","大分県","宮崎県","鹿児島県","沖縄県"
		};

		List<String> addrList = new ArrayList<String>();

		for (int idx = 0; idx < addrArr.length; idx++) {
			addrList.add(addrArr[idx]);
		}

		return addrList;
	}
}
